package com.link.base.base.codereview.controller;

import com.link.base.base.codereview.model.DirectoryTree;
import com.link.base.base.codereview.model.SysCodeReview;
import com.link.base.base.codereview.service.SysCodeReviewService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.model.ReturnCodeConstants;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 代码审核管理-专用于代码审核功能
 *
 * @author 谢鑫
 * @date 2019/09/25 09:59
 */
@Controller
@RequestMapping("/link/sysCodeReview")
public class SysCodeReviewController extends BasicController<SysCodeReview> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private SysCodeReviewService sysCodeReviewService;

    @Override
    public BasicService<SysCodeReview> getBasicService() throws Exception {
        return sysCodeReviewService;
    }

    /**
     * 查询目录树
     *
     * @author LiYang
     * @date 20191031
     * @param sysCodeReview
     * @param qps
     * @return
     */
    @RequestMapping(value = "/getDirectoryTree", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getDirectoryTree(@JsonParam SysCodeReview sysCodeReview, @JsonParam QueryParams qps) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            BasicModel.transformClass(sysCodeReview, qps);
            DirectoryTree directoryTree = sysCodeReviewService.getDirectoryTree(sysCodeReview);
            result.put("success", true);
            result.put("result", directoryTree);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 代码审计（审计按钮）接口
     *
     * @author 李杨
     * @date 2019/10/31
     */
    @RequestMapping(value = "/approval", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> approval(@JsonParam SysCodeReview sysCodeReview, @JsonParam QueryParams qps) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            BasicModel.transformClass(sysCodeReview, qps);
            sysCodeReviewService.approvalCodeReview(sysCodeReview);
            SysCodeReview newRow = sysCodeReviewService.queryById(sysCodeReview);

            result.put("newRow", newRow);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 通过全表的去重查询查询所有项目
     *
     * @author 李杨
     * @date 2019/10/31
     */
    @RequestMapping(value = "/queryProject", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryProj(@JsonParam SysCodeReview sysCodeReview, @JsonParam QueryParams qps) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            BasicModel.transformClass(sysCodeReview, qps);
            List<SysCodeReview> list = sysCodeReviewService.queryProj(sysCodeReview);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", list.size());
            result.put("code", ReturnCodeConstants.SUCCESS);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
